##================================================##
##  False Consciousness or Class Awareness?       ##
##  Local Income Inequality, Personal Economic    ##
##  Position, and Belief in American Meritocracy  ##
##                                                ##
##            R  Replication File                 ##
##                                                ##
##  Authors:      Benjamin Newman                 ##
##                Christopher Johnston            ##
##                Patrick Lown                    ##
##                                                ##
##  Prepared by:  Patrick Lown                    ##
##                                                ##
##================================================##

##  Replications Use R version 2.15.2 (2012-10-26)

library(foreign) 
library(car)
library(Zelig) #Zelig_4.1-0 dependent on lme4_0.999375-39, zoo_1.7-6, and sandwich_2.2-9 
library(ZeligMultilevel) #ZeligMultilevel_0.7-0

setwd("Working Directory") #Paste working directory of data files

##---------------##
##    TABLE 1    ##
##---------------##

    ##  Whites
        table1_data<-read.dta("Meritocracy Replication Data - Table 1.dta")
        table1_data<-table1_data[table1_data$white==1,]
        
        z.out <- zelig(formula=meritocracy~ginicnty+income_i+ginicntyXincome_i+income_cnty+
                         black_cnty+perc_bush04+pop_cnty+educ_i+age_i+gender_i+unemp_i+union_i+partyid_i+
                         ideo_i+attend_i+survid2006+survid2007+survid2009+tag(1+income_i|fips),
                        data=table1_data,model="logit.mixed")
        summary(z.out)

    ##  Nonwhites
        table1_data<-read.dta("Meritocracy Replication Data - Table 1.dta")
        table1_data<-table1_data[table1_data$white==0,]
        
        z.out <- lmer(formula=meritocracy~ginicnty+income_i+ginicntyXincome_i+income_cnty+
                         black_cnty+perc_bush04+pop_cnty+educ_i+age_i+gender_i+unemp_i+union_i+partyid_i+
                         ideo_i+attend_i+survid2006+survid2007+survid2009+(1+income_i|fips),
                       data=table1_data)
        summary(z.out)

        
##---------------##
##    TABLE 2    ##
##---------------##

  ##  See STATA Replication Files

        
##---------------##
##    TABLE 3    ##
##---------------##
    table3_data<-read.dta("Meritocracy Replication Data - Table 3.dta")
    table3_data<-table3_data[table3_data$white==1,]
    
    z.out <- zelig(formula=havenot2~ginicnty+income_i+ginicntyXincome_i+income_cnty+
                     black_cnty+perc_bush04+pop_cnty+educ_i+age_i+gender_i+unemp_i+union_i+
                     partyid_i+ideo_i+attend_i+tag(1+income_i|fips), data=table3_data, model="logit.mixed")
    summary(z.out)
    
    
    
    